package cn.tedu.egweibo3.controller;

import cn.tedu.egweibo3.common.response.JsonResult;
import cn.tedu.egweibo3.common.response.StatusCode;
import cn.tedu.egweibo3.mapper.WeiboMapper;
import cn.tedu.egweibo3.pojo.dto.WeiboDTO;
import cn.tedu.egweibo3.pojo.entity.Weibo;
import cn.tedu.egweibo3.pojo.vo.UserVO;
import cn.tedu.egweibo3.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo3.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.debug("weiboDTO"+weiboDTO);
        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO!=null){
            Weibo weibo =new Weibo();
            BeanUtils.copyProperties(weiboDTO,weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(userVO.getId());
            weiboMapper.insert(weibo);
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }
            return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @ApiOperation("微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectIndex();
        return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboIndexVOList);
    }

    @ApiOperation("微博详情页")
    @GetMapping("selectById")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "String")
    })
    public JsonResult selectById(Long id,String username){
        log.debug("id="+id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboDetailVO);
    }
}
